package com.aaa.controller.system;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.system.Dept;
import com.aaa.entity.Result;
import com.aaa.entity.system.PageOrder;
import com.aaa.service.system.DeptService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author ：肖成杰（1752167034@qq.com）
 * @date ：Created in 2022/8/10 8:33
 * @description：
 * @modified By：
 * @version:
 */
@Controller
@RequestMapping("dept")
public class DeptServlet extends BaseController {
    @Autowired
    private DeptService deptService;

    /**
     *  @create by: 肖成杰
     *  @description:
     *  @create time: 2022/9/2 21:14
     *  @params 查询所有数据 用在下拉框中
     *  @return com.aaa.entity.Result
     *  */
    @RequestMapping("query.do")
    @ResponseBody
    public Result queryAll(){
        List<Dept> depts = deptService.quaryAll();
        if(ObjectUtil.isNotEmpty(depts)){
            return success(depts);
        }
        return fail();
    }

    /**
     *  @create by: 肖成杰
     *  @description:
     *  @create time: 2022/9/2 21:14
     *  @params 查询所有数据附带条件查询
     *  @return com.aaa.entity.Result
     *  */
    @RequestMapping("queryAll.do")
    @ResponseBody
    public String query(PageOrder pageOrder, Dept dept){
        Page page = deptService.query(pageOrder, dept);
        JSONConfig jsonConfig = JSONConfig.create();
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        String jsonStr = JSONUtil.toJsonStr(success(page.getTotal(),page.getResult()), jsonConfig);
        return jsonStr;
    }

    /**
     *  @create by: 肖成杰
     *  @description:删除数据
     *  @create time: 2022/9/2 21:15
     *  @params deptId
     *  @return com.aaa.entity.Result
     *  */
    @RequestMapping("delete.do")
    @ResponseBody
    public Result delete(int[] deptId){
        boolean b = deptService.delete(deptId);
        if(b){
            return  success("删除成功");
        }
        return fail("删除失败");
    }

    /**
     *  @create by: 肖成杰
     *  @description:
     *  @create time: 2022/9/2 21:16
     *  @params dept实体类包含加入语句的信息
     *  @return com.aaa.entity.Result
     *  */
    @RequestMapping("insert.do")
    @ResponseBody
    public Result insertUser(Dept dept){
        boolean insert = deptService.insertDept(dept);
        if(insert){
            return success();
        }else {
            return fail();
        }
    }

    /**
     * 修改部门信息
     * @param dept
     * @return
     */
    @RequestMapping("update.do")
    @ResponseBody
    public Result updateUser(Dept dept){
        boolean updateUser = deptService.updateDept(dept);
        if(updateUser){
            return success();
        }else {
            return fail();
        }
    }

    /**
     * 表单验证 查询部门名称是否被占用
     * @param deptName
     * @return
     */
    @RequestMapping("queryByName.do")
    @ResponseBody
    public Result queryByName(String deptName){
        List<Dept> queryByName= deptService.queryByName(deptName);
        if(ObjectUtil.isNotEmpty(queryByName)){
            return fail("该部门名称已经被使用");
        }else {
            return success("可用的部门名称");
        }
    }



    //用户查询页面
    @RequestMapping("toShowDept.do")
    public ModelAndView toShowUser(ModelAndView modelAndView){
        modelAndView.setViewName("system/showDept");
        return modelAndView;
    }

}